/*
 * @Descripttion: 
 * @version: 
 * @Author: kl2018
 * @Date: 2020-04-30 17:24:44
 * @LastEditors: kl2018
 * @LastEditTime: 2020-06-04 16:42:43
 */
import { observable, action, toJS } from 'mobx';
import { Tools, Toast } from 'weaver-mobile';
import { ListViewPage, RouteLayout } from 'weaver-mobile-page';
import * as Apis from "../../apis/docLedgerApi/docLedgerApi";

export default class DocLedger {
  @observable list = new ListViewPage.Store({ route: '/docLedger' });
  @observable layout = new RouteLayout.Store({ route: '/docLedger', id: "recDocDetail" });
  @observable userId = "";
  @observable loading = false;
  @observable requestedSubWF = false;
  @observable allSubWF = {}

  @action.bound
  getData(param, current, refresh = false) {
    this.requestedSubWF = false;
    this.loading = true;
    const params = {
      ...param,
    }
    Apis.getList(params).then(data => {
      if (data.sessionkey) {
        this.list.getData({ dataKey: data.sessionkey, current: current }, (datas) => {
          this.getSubWF(datas.datas)
          this.loading = false;
        }, refresh);
      }
    })
  }

  @action.bound
  getFormData(data, formData) {
    for (let key in data) {
      if (typeof data[key] == "string") {
        formData[key] = data[key];
      } else if (typeof data[key] == "object") {
        if (data[key].length) {
          let ids = [];
          for (let i in toJS(data[key])) {
            ids.push(data[key][i].id);
          }
          formData[key] = ids.join(",");
        } else {
          this.getFormData(data[key], formData)
        }
      }
    }
  }

  //获取userid
  @action.bound
  getUserId() {
    Apis.getUserId().then(data => {
      if (data.api_status) {
        this.userId = data.userId;
      }
    })
  }

  @action.bound
  setMobxState(params = {}) {
    Object.keys(params).forEach(key => {
      this[key] = params[key]
    })
  }

  @action.bound
  getSubWF(datas) {
    let hasSubWFRequestidArr = []
    datas.forEach(item => {
      if (item.subrequestInfospan === '1') {
        hasSubWFRequestidArr.push(item.requestid)
      }
    })
    if (hasSubWFRequestidArr.length === 0) {
      this.loading = false;
      return;
    }
    const params = {
      requestids: hasSubWFRequestidArr.join(',')
    }
    Apis.getSubWF(params).then(datas => {
      this.requestedSubWF = true
      this.allSubWF = { ...this.allSubWF, ...datas }
    })
  }

}